﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Messaging;
using System.ComponentModel;
using System.Net;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Friedlich.Common;

namespace Friedlich.Delicious
{
    public class DeliciousWatcher
    {


        public string Url { get; set; }



        private void ReceivePost(string url)
        {
            getPost(url);
        }

        public string AccessToken { get; set; }
        public DeliciousWatcher(string accessToken) {
            this.AccessToken = accessToken;
        }

        private delegate void ReceivePostDelegate(string url);

        private void CompletedCallback(IAsyncResult result)
        {
            ReceivePostDelegate worker = (ReceivePostDelegate)((AsyncResult)result).AsyncDelegate;
            AsyncOperation async = (AsyncOperation)result.AsyncState;
            worker.EndInvoke(result);            

            DeliciousEventArgs completedArgs = new DeliciousEventArgs(null, false, null);

            async.PostOperationCompleted(
            delegate(object e) { OnPostReceived((AsyncCompletedEventArgs)e); }, (AsyncCompletedEventArgs)completedArgs);

        }


        private DeliciousPost getPost(string url) {
            DeliciousPost post = null;
            if (!string.IsNullOrEmpty(url))
            {
                post.Url = url;

                Uri feedUri = new Uri(url);

                HttpWebRequest feedRequest = (HttpWebRequest)WebRequest.Create(feedUri);
                if ((feedRequest.IfModifiedSince.CompareTo(DateTime.Now) == 0))
                {
                    XDocument feedDoc = XDocument.Load(url);
                    XElement titleElm = feedDoc.Element("title");
                    if (titleElm != null) post.Title = titleElm.Value;
                    DeliciousFactory factory = new DeliciousFactory();
                    TagCollection suggestedTags = factory.GetSuggestedTags(url);


                }
            }
            return post;
        }

        private bool quit = false;

        

        

        public void ListenForPost(string url)
        {
            this.Url = url;
            ReceivePostDelegate listener = new ReceivePostDelegate(ReceivePost);
            AsyncCallback completedCallback = new AsyncCallback(CompletedCallback);
            AsyncOperation async = AsyncOperationManager.CreateOperation(null);
            listener.BeginInvoke(url, completedCallback, async);

                           
            
                
        }

        public event AsyncCompletedEventHandler PostReceived;

        protected virtual void OnPostReceived(AsyncCompletedEventArgs e)
        {
            if (PostReceived != null)
            {
                {
                    PostReceived(this, e);
                }
            }

        }
    }
}
